package com.mogujie.tt.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mogujie.tt.DB.sp.GesturespasswordSp;
import com.mogujie.tt.R;
import com.mogujie.tt.app.IMApplication;
import com.mogujie.tt.imservice.event.LoginEvent;
import com.mogujie.tt.imservice.event.OpenpwdEvent;
import com.mogujie.tt.ui.base.QZBaseActivity;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/11/4.
 */
public class NumPwdActivity extends QZBaseActivity {

    private EditText numEdit;
    private Button qx,button;
    private String sign="",numedi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater.from(this).inflate(R.layout.numpwdactivity, topContentView);
        showTopInformation("选择您的数字密码");
        findView();
        onclick();
    }

    private void findView() {

        numEdit = (EditText) findViewById(R.id.numEdit);
        qx = (Button) findViewById(R.id.button);
        button = (Button) findViewById(R.id.button2);
        button.setEnabled(false);

    }

    private void onclick() {

        numedi = numEdit.getText().toString();

        numEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length()==6){
                    button.setEnabled(true);
                }
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numedi = numEdit.getText().toString();
                if (sign=="") {
                    button.setText("确定");
                    sign=numedi+"";
                    numEdit.setText("");
                    button.setEnabled(false);
                }else{
                    System.out.println("111111111" + sign+"   "+numedi);
                    if (sign.equals(numedi)){
                        GesturespasswordSp.set(NumPwdActivity.this, GesturespasswordSp.getNUM(), sign);
                        IMApplication.getInstance().getLockPatternUtils().Lockdelete();
                        triggerEvent(OpenpwdEvent.SUCCEED);
                        startActivity(new Intent(NumPwdActivity.this, GesturesActivity.class));
                        finish();
                    }else{
                        numEdit.setText("");
                        Toast.makeText(getApplicationContext(),"两次结果不同",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 实现自身的事件驱动
     * @param event
     */
    public void triggerEvent(OpenpwdEvent event) {
        EventBus.getDefault().postSticky(event);
    }

    private void showTopInformation(String test){
        if(test == null){
            return;
        }
        setTopBarDisplay();
        setCenterText(test);
    }
    public void onClickExit(View view){
        finish();
    }
}
